package entities;

import graphics.WindowManager;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JPanel;
import willispace.WilliSpace;

/**
 * Base abstract class for all entities and tiles. Defines mouse listener
 * @author mateusz
 */
public abstract class MyPanel extends JPanel implements MouseListener {

    /**
     * image of tile/entity
     */
    protected Image img;

    /**
     * Creates myPanel
     * @param position  position on displayed panel
     * @param img       image
     */
    public MyPanel(Point position, Image img) {
        this.img = img;

        Dimension size = new Dimension(img.getWidth(null), img.getHeight(
                null));
        setLayout(null);
        setLocation(position);
        setPreferredSize(size);
        setMinimumSize(size);
        setMaximumSize(size);
        setSize(size);
        setOpaque(false);
        // add listeners
        addMouseListener(this);
    }

    @Override
    public void show() {
        setVisible(true);
    }

    @Override
    public void hide() {
        setVisible(false);
    }

    /**
     * 
     * @return image of tile/entity
     */
    public Image getDefaultImage() {
        return img;
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        if (!WilliSpace.isPaused()) {
            final MyPanel me = this;

            java.awt.EventQueue.invokeLater(new Runnable() {

                @Override
                public void run() {
                    WindowManager.entityWindow.call(me);
                }
            });
        }
    }

    @Override
    public void mousePressed(MouseEvent e) {
    }

    @Override
    public void mouseReleased(MouseEvent e) {
    }

    @Override
    public void mouseEntered(MouseEvent e) {
    }

    @Override
    public void mouseExited(MouseEvent e) {
    }

    @Override
    public void update(Graphics g) {
        paintComponent(g);
    }
}
